package com.wyvoid.forum.web.action.forum;

import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.Validate;
import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.web.search.SearchBean;
import com.wyvoid.forum.service.IPostService;
import com.wyvoid.forum.service.ITopicService;
import com.wyvoid.forum.domain.Post;
import com.wyvoid.forum.domain.Topic;
import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernatePage;

/**
 * User: cjp
 * Date: 2007-7-11
 * Time: 22:05:01
 */
@UrlBinding("/search.do")
public class SearchAction extends BaseAction {
    private int curPage;

    @SpringBean
    private IPostService postService;

    @SpringBean
    private ITopicService topicService;
    private HibernatePage<Post> posts;
    private HibernatePage<Topic> topics;

    @ValidateNestedProperties({
    @Validate(field = "key", required = true, minlength = 1, maxlength = 16)
            })
    private SearchBean searchBean;


    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public void setPostService(IPostService postService) {
        this.postService = postService;
    }


    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }


    public void setSearchBean(SearchBean searchBean) {
        this.searchBean = searchBean;
    }


    public SearchBean getSearchBean() {
        return searchBean;
    }

    public HibernatePage<Post> getPosts() {
        return posts;
    }


    public HibernatePage<Topic> getTopics() {
        return topics;
    }

    @DefaultHandler
    @DontValidate
    public Resolution searchForm() {
        return getBBSResolution("searchForm");
    }


    public Resolution search() {
        if (curPage == 0) {
            curPage = 1;
        }

        Pagination pagination = new Pagination();
        pagination.setPageNumber(curPage);
        pagination.setPageSize(20);

        if (searchBean.getSearchType() == 0) {
            topics = topicService.searchTopics(searchBean, pagination);
            return getBBSResolution("searchTopicResults");
        } else {
            posts = postService.searchPosts(searchBean, pagination);
            return getBBSResolution("searchPostResults");
        }
    }

    @DontValidate
    public Resolution newestTopics() {
        searchBean = new SearchBean();
        searchBean.setBeforeTimes(86400);
        searchBean.setAscdesc("desc");
        return search();
    }

    @DontValidate
    public Resolution digest() {
        searchBean = new SearchBean();
        searchBean.setSearchFilter(1);
        searchBean.setAscdesc("desc");
        return search();
    }
}
